<template>
  <div class="app-container">
    <!--查询表单-->
    <el-form :inline="true" class="demo-form-inline">
      <!-- 所属分类：级联下拉列表 -->
      <el-form-item label="医院名称">
        <el-autocomplete
          v-model="hospitalSetQueryVo.hosname"
          :fetch-suggestions="querySearch"
          :trigger-on-focus="false"
          class="inline-input"
          placeholder="请输入医院名称"
          value-key="hosname"/>
      </el-form-item>
      <el-form-item label="医院编号">
        <el-autocomplete
          v-model="hospitalSetQueryVo.hoscode"
          :fetch-suggestions="querySearch"
          :trigger-on-focus="false"
          class="inline-input"
          placeholder="请输入医院编号"
          value-key="hoscode"/>
      </el-form-item>
      <!-- 按钮 -->
      <el-button type="primary" icon="el-icon-search" @click="getListByHospitalSetQueryVo()">查询</el-button>
      <el-button type="default" @click="resetData()">清空</el-button>
    </el-form>
    <!-- 工具条 -->
    <div style="margin-bottom: 10px;">
      <el-button type="danger" size="mini" @click="batchDeleteHospSetByIdList()">批量删除</el-button>
    </div>
    <!-- 表格 -->
    <el-table :data="currentHospSetList" border stripe element-loading-text="数据加载中" @selection-change="multiSelectChange">  <!--【注意：组件这里的方法不能加括号，否则会出错，达不到前端效果】-->
      <el-table-column type="selection" label="全选"/>
      <el-table-column type="index" label="序号" width="50" align="center"/>
      <el-table-column prop="hosname" label="医院名称"/>
      <el-table-column prop="hoscode" label="医院编号"/>
      <el-table-column prop="apiUrl" label="api基础路径"/>
      <el-table-column prop="contactsName" label="联系人姓名"/>
      <el-table-column prop="contactsPhone" label="联系人手机"/>
      <el-table-column label="状态" width="80">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status === 1 ? 'success' : 'danger'" effect="light">{{ scope.row.status === 1 ? '可用' : '不可用' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="258" align="center">
        <template slot-scope="scope">
          <!-- 【路由跳转】 -->
          <router-link :to="'/hospSet/edit/'+scope.row.id">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit">修改
            </el-button>
          </router-link>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="removeDataById(scope.row.id)">删除
          </el-button>
          <el-button
            v-if="scope.row.status === 1"
            type="warning"
            size="mini"
            icon="el-icon-edit"
            @click="lockHostSetById(scope.row.id,0)">锁定
          </el-button>
          <el-button
            v-if="scope.row.status === 0"
            type="success"
            size="mini"
            icon="el-icon-check"
            @click="lockHostSetById(scope.row.id,1)">解锁
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <el-pagination
      :current-page="current"
      :total="total"
      :page-size="limit"
      :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
      style="padding: 30px 0; text-align: center;"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="changePageSize"
      @current-change="changeCurrentPage"/>
  </div>
</template>

<script>
import hospSetApi from '@/api/hospSet'

export default {

  data() {
    return {
      current: 1, // 当前页
      limit: 5, // 每页显示10条数据
      total: 0, // 总记录数
      currentHospSetList: [], // 当前页的医院设置列表
      hospSetList: [], // 医院设置的所有列表
      hosCodeList: [], // 医院编号列表
      hospitalSetQueryVo: {}, // 搜索框查询条件对象
      mutilSelectInfo: [], // 批量删除
      idList: [] // 选中的id列表
    }
  },

  created() {
    this.getHospSetPageList()
    this.getHosCode()
  },

  methods: {
    // 分页获取医院设置列表
    getHospSetPageList() {
      hospSetApi.findPageHospSet(this.current, this.limit, this.hospitalSetQueryVo).then((response) => {
        // console.log(response.data)
        this.currentHospSetList = response.data.pageHospitalSetList.records
        this.total = response.data.pageHospitalSetList.total
      })
    },
    // 获取讲师姓名匹配建议(这里的参数都是组件帮封装好的)
    querySearch(queryString, callback) {
      hospSetApi.getNameListByKeyword(queryString).then((response) => {
        console.log(queryString) // 输入框中输入的数据
        callback(response.data.hosNameList)
        console.log(response.data)
      })
    },
    // 根据搜索框选中的医院名称获取该医院编号
    getHosCode(hosname) {
      hospSetApi.getHosCodeByHosName(hosname).then((response) => {
        console.log(response.data.hosCodeList)
      })
    },

    // 根据搜索框条件进行查询列表
    getListByHospitalSetQueryVo() {
      hospSetApi.findPageHospSet(this.current, this.limit, this.hospitalSetQueryVo).then((response) => {
        this.currentHospSetList = response.data.pageHospitalSetList.records
        this.total = response.total
      })
    },
    // 清空搜索框
    resetData() {
      this.hospitalSetQueryVo = {}
      this.getHospSetPageList()
    },
    // 当多选框发生变化时触发
    multiSelectChange(selection) {
      this.mutilSelectInfo = selection
      this.mutilSelectInfo.forEach(item => {
        console.log(item.id)
      })
    },
    // 批量删除医院设置列表
    batchDeleteHospSetByIdList() {
      // 判断是否选中记录
      if (this.mutilSelectInfo.length === 0) {
        this.$message({
          type: 'warning',
          message: '请先选择要删除的医院设置'
        })
        return // 注意这里需要return，否则当我们没有选中删除医院设置时，会先弹出此处提示信息，然后又会接着弹出下面代码的提示信息
      }
      // 已经选中要删除的课程了
      this.$confirm('此操作将永久删除该医院设置, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 通过返回的selection对象信息获取到选中的讲师ID
        this.mutilSelectInfo.forEach(item => {
          // 把选中的ID放进入idList中
          this.idList.push(item.id)
        })
        hospSetApi.batchRemoveByIdList(this.idList).then((response) => {
          if (response.code === 200) {
            // 删除成功，并重新刷新表格
            // 显示提示信息
            this.$message.success(response.message)
            // 当删除提示框完成后，刷新列表页面
            this.getHospSetPageList()
          } else {
            this.$message.warn(response.message)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 切换分页
    changeCurrentPage(current) {
      this.current = current
      this.getHospSetPageList()
    },
    // 切换每页显示数量limit参数
    changePageSize(limit) {
      this.limit = limit
      this.getHospSetPageList()
    },
    // 根据id删除医院设置
    removeDataById(id) { // 这里的id是饿了么组件通过scope.row.id帮我们获取的
      this.$confirm('此操作将永久删除该讲师, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        hospSetApi.removeHospById(id).then((response) => {
          // 显示提示信息
          this.$message({
            type: 'success',
            message: '删除成功'
          })
          // 当删除提示框完成后，刷新列表页面
          this.getHospSetPageList()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 修改医院设置的锁定状态
    lockHostSetById(id, status) { // 这里的id是饿了么组件通过scope.row.id帮我们获取的
      if (status === 0) {
        hospSetApi.lockHospSet(id, status).then((response) => {
          this.$message.success('锁定成功')
          // 当删除提示框完成后，刷新列表页面
          this.getHospSetPageList()
        })
      } else {
        hospSetApi.lockHospSet(id, status).then((response) => {
          this.$message.success('解锁成功')
          // 当删除提示框完成后，刷新列表页面
          this.getHospSetPageList()
        })
      }
    }

  }
}

</script>
